package com.nikmesoft.android.referendumfriend.service;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nikmesoft.android.referendumfriend.models.ItemHobies;
import com.nikmesoft.android.referendumfriend.models.userhobies;

public class MyHobbiesHandler extends DefaultHandler {
	private final static String ERRORCODE_XML = "ErrorCode";
	private final static String ERRORCODE_ID_XML = "errorID";
	private final static String ERRORCODE_MSG_XML = "errorMsg";
	private final static String ID = "id";
	private final static String NAME = "name";
	private final static String ICON = "icon";
	private final static String ITEM = "item";
	
	
	private StringBuilder builder;
	public  static ItemHobies item = null;
	private ErrorCode errorCode;
	private userhobies result=new userhobies();

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(new String(ch, start, length));
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if (this.errorCode != null) {
			if (localName.equalsIgnoreCase(ERRORCODE_ID_XML)) {
				errorCode.setErrorID(builder.toString().trim());
			} else if (localName.equalsIgnoreCase(ERRORCODE_MSG_XML)) {
				errorCode.setErrorMsg(builder.toString().trim());
			}
		}else if (localName.equalsIgnoreCase(ID)) {
			
			item.setId(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(NAME)) {
			item.setName(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(ICON)) {
			item.setIcon(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(ITEM)) {
			result.add(item);
		}
		builder.setLength(0);

	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if (localName.equalsIgnoreCase(ERRORCODE_XML)) {
			this.errorCode = new ErrorCode();
		}
		else if (localName.equalsIgnoreCase(ITEM)) {
	    	   item=new ItemHobies();
			}
	}

	public userhobies getResult() {
		result.setErr(errorCode);
		return result;
	}

}
